/**
 * Copyright 2011-2011 - Members of the Chaupal Community
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package net.chaupal.Impl.BoostedObject;

import net.chaupal.Impl.Tools.Soundness;
import net.chaupal.Impl.Unique.Unique;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provides an improved version of the delivered Java {@link java.lang.Object}.
 */
public class BoostedObject implements Boosted {
    
    private static final Logger LOG
            = LoggerFactory.getLogger(BoostedObject.class);
    
    // Attributes
    
    private final long instanceID = Unique.nextLong();
    private final int hash;
    
    private String name;

    /**
     * Default constructor.
     */
    public BoostedObject() {
        
        this("");
        
    }
    
    public BoostedObject(String name) {

        // Sanity
        Soundness.nullParameter(LOG, name, "Invalid null name");
        
        // Initialization
        this.name = name;
        this.hash = 79 * 7 + (int) (this.instanceID ^ (this.instanceID >>> 32));
        
    }
    
    /**
     * Copy constructor.
     * 
     * @param toCopy the object to copy
     */ 
    public BoostedObject(BoostedObject toCopy) {
        
        this(toCopy.name);
        
    }

    /**
     * Sets a name to this object.
     * 
     * @param name a name for this object
     */
    @Override
    public final void setName(String name) {
        
        // Sanity
        Soundness.nullParameter(LOG, name, "Invalid null name");

        // Initialization
        this.name = name;
        
    }

    /**
     * Provides the name for this object.
     * 
     * @return the name for this object
     */
    @Override
    public final String getName() {
        return this.name;
    }
    
    /**
     * Provides an instance id unique to this object instance. This ID is
     * created with the help of an {@link AtomicLong}.
     * 
     * @return 
     */
    @Override
    public final long getInstanceID() {
        return this.instanceID;
    }
    
    @Override
    public int hashCode() {
        return hash;
    }

    @Override
    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    public boolean equals(Object obj) {
        
        return this == obj;
        
    }
    
    /**
     * This overrreding is equivalent to calling the copy constructor of this
     * object. It is made final to force users to not use clone() and to use
     * copy constructors instead.
     * 
     * @return a copy of this object
     */
    @Override
    public final BoostedObject clone() {
        
        return new BoostedObject(this);
        
    }
    
}